//
//  MemberWelfareChoiceTableViewCell.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/30.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "MemberWelfareChoiceTableViewCell.h"
#import "MemberWelfareChoiceContentTableViewCell.h"
#import "PopupWebView.h"
#import <AudioToolbox/AudioToolbox.h>

static NSString *choiceCellID = @"choiceCellID";

@interface MemberWelfareChoiceTableViewCell () <UITableViewDelegate, UITableViewDataSource>
{
    SystemSoundID scoreClickBtnID;   // 自己设置的音效 (系统的振动音效 - kSystemSoundID_Vibrate)
}
@property (strong, nonatomic) UITableView *choiceTableView;

@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation MemberWelfareChoiceTableViewCell


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self.contentView addSubview:self.choiceTableView];
        
        __weak typeof(self) weakSelf = self;
        [self.choiceTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(weakSelf.contentView.mas_left);
            make.right.mas_equalTo(weakSelf.contentView.mas_right);
            make.top.mas_equalTo(weakSelf.contentView.mas_top);
            make.bottom.mas_equalTo(weakSelf.contentView.mas_bottom);
            
        }];
        
            AudioServicesCreateSystemSoundID((__bridge CFURLRef)([NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"5a2e388b2886c.wav" ofType:@""]]), &scoreClickBtnID);
    }
    return self;
}

//- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
//
//    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
//
//
//    }
//
//    return self;
//}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *sectionHeader = [[UIView alloc] init];
    sectionHeader.backgroundColor = WhiteColor;
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"会员热销榜单";
    label.textColor = Color333333;
    label.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:jisuan(32)];
    label.textAlignment = NSTextAlignmentCenter;
    [sectionHeader addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(sectionHeader.mas_centerX);
        make.top.mas_equalTo(sectionHeader.mas_top).with.offset(jisuan(30));
    }];
    UILabel *desL = [[UILabel alloc] init];
    desL.text = @"每日更新，惊喜不断";
    desL.textColor = Color999999;
    desL.font = Font(20);
    desL.textAlignment = NSTextAlignmentCenter;
    [sectionHeader addSubview:desL];
    [desL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(sectionHeader.mas_centerX);
        make.top.mas_equalTo(label.mas_bottom).with.offset(jisuan(10));
    }];
    
    return sectionHeader;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return jisuan(142);
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return jisuan(220);
    
}

#pragma mark -delegate \ datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MemberWelfareChoiceContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:choiceCellID];
    
    if (!cell) {
    
        cell = [[MemberWelfareChoiceContentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:choiceCellID];
        
    }
    
    cell.type = indexPath.row;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.goodListModel = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    PopupWebView *popppview = [[PopupWebView alloc] init];
    popppview.hidden = YES;
    popppview.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    [[UIApplication sharedApplication].keyWindow addSubview:popppview];
    
    SupportCategoryGoodListModel *model = self.dataArray[indexPath.row];
    NSString *string = [NSString stringWithFormat:@"{\"user_id\" : \"%@\"}",[RSA encryptString:[CommonUserInfo sharedInstance].user_id publicKey:PublicKey]];
    [PPNetworkHelper POST:@"welfare/browse/index" parameters:@{@"json":string} success:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 1) {
            [popppview loadWithHtml:responseObject[@"data"][@"jiangli"]];
            popppview.hidden = NO;
            AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
            AudioServicesPlaySystemSound(self->scoreClickBtnID);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
                [popppview removeFromSuperview];
                //                [weakSelf.navigationController pushViewController:ctr animated:YES];
                NSDictionary *dict = @{@"goodId":model.goodId};
                [[NSNotificationCenter defaultCenter] postNotificationName:@"PUSHTOGOODDETAIL" object:nil userInfo:dict];
            });
        } else {
            NSDictionary *dict = @{@"goodId":model.goodId};
            [[NSNotificationCenter defaultCenter] postNotificationName:@"PUSHTOGOODDETAIL" object:nil userInfo:dict];
        }
    } failure:^(NSError *error) {
        NSDictionary *dict = @{@"goodId":model.goodId};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"PUSHTOGOODDETAIL" object:nil userInfo:dict];
    }];
    
}


#pragma mark -lazy
- (UITableView *)choiceTableView {
    
    if (!_choiceTableView) {
        
        _choiceTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _choiceTableView.delegate = self;
        _choiceTableView.dataSource = self;
        _choiceTableView.scrollEnabled = NO;
        _choiceTableView.estimatedRowHeight = 0;
        _choiceTableView.estimatedSectionFooterHeight = 0;
        _choiceTableView.estimatedSectionHeaderHeight = 0;
        _choiceTableView.showsVerticalScrollIndicator = NO;
        _choiceTableView.showsHorizontalScrollIndicator = NO;
        _choiceTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        if (@available(iOS 11.0, *)) {
            
            _choiceTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _choiceTableView;
}

- (void)reloadCellArray:(NSArray *)array {
    self.dataArray = array;
    [self.choiceTableView reloadData];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

@end
